/*
 * This packet is the response from the server to the client for the security
 * level setting request.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class BoardSendPacket extends Packet {

    int board[][];
    short board_width;
    short board_height;
    int player_number;

    public BoardSendPacket( short game_name, short game_version,
                            int player_number, int board[][] ) {

        /*Set up the header*/
        message_type = MSG_BOARD_SEND;
        this.game_name = game_name;
        this.game_version = game_version;
        
        this.board_height = (short)board.length;
        this.board_width = (short)board[0].length;
        
        this.player_number = player_number;
        
        this.board = board;
        
        /*data headers and the board itself*/
        data_area_size = 4 + 4 + 4 + (board_width * board_height * 4);
    }

    public BoardSendPacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        board_width = buf.getShort();
        board_height = buf.getShort();
        
        buf.getInt(); /*reserved space*/

        player_number = buf.getInt();
        
        board = new int[board_width][board_height];
        
        for ( int h = 0; h < board_height; h++ ) {
            for ( int w = 0; w < board_width; w++ ) {
                board[w][h] = buf.getInt();
            }
        }
    }
    
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        
        buf.putShort( board_width );
        buf.putShort( board_height );
        
        buf.putInt( 0 ); /*reserved space*/

        buf.putInt( player_number );
        
        for ( int h = 0; h < board_width; h++ ) {
            for ( int w = 0; w < board_height; w++ ) {
                buf.putInt( board[w][h] );
            }
        }
        
        return buf.array();
    }
    
    public String toString() {
        String ret = super.toString();
        ret = ret + "\nboard_width="+board_width+" board_height="+board_height;
        ret = ret + "\nzero=0";
        ret = ret + "\nplayer_number="+player_number;
        
        ret = ret + "\nboard=";
        for ( int h = 0; h < board_width; h++ ) {
            String line = "";
            for ( int w = 0; w < board_height; w++ ) {
                line = line + "  " + board[w][h];
            }
            ret = ret + "\n" + line;
        }
        return ret;
    }
    
    public int[][] get_board() { return board; }
    public short get_board_width() { return board_width; }
    public short get_board_height() { return board_height; }
    public int get_player_number() { return player_number; }
}
